﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using YT.Controls.GridColumn;
using YT.DB;
using YT.Commons;
using YT.Modules;

namespace YT.XH.IgE
{
    public partial class frmIgESetDetectLabel : Form
    {
        private DBAccess mdbcon = new DBAccess(false, false);
        public FormulaType mFormulaType = new FormulaType();
        private string mstrHdrRecID = "";
        public frmIgESetDetectLabel(string strHdrRecID)
        {
            InitializeComponent();
            mstrHdrRecID = strHdrRecID;
        }

        private bool postedalready = false;
        public bool Postedalready
        {
            get {
                using (DB.DBAccess dbcon = new DBAccess(false, false))
                {
                    string strSQL = "Select Posted From IgEDetectHdr Where RecID='" + mstrHdrRecID + "' ";
                    DataRow drRow = dbcon.GetDataFirstRow(strSQL);
                    if (drRow == null)
                    {
                        return false;
                    }
                    else
                    {
                        return CvtFunc.ToBoolean ( drRow["Posted"]) ;
                    }
                }
            
                }
            set { postedalready = value; }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }

        private void frmIgESetDetectLabel_Load(object sender, EventArgs e)
        {
            this.dgvSet.EditMode = DataGridViewEditMode.EditOnEnter;
            this.Text = "检测版设置 -- 选择患者";
            FillGrid();
            this.dgvSet.Columns[colCols.Name].ReadOnly = true;
            this.dgvSet.Columns[colRow.Name].ReadOnly = true;
            this.dgvSet.Columns[colCharRow.Name].ReadOnly = true;
            this.dgvSet.Columns[colRow.Name].Visible = false;
            this.dgvSet.Columns["colSickRecID"].ReadOnly = true;
            if (Postedalready)
            {
                this.dgvSet.Columns[colSickName.Name].ReadOnly = true;
                this.dgvSet.Columns[colSickRecID.Name].ReadOnly = true;
                this.dgvSet.Columns[colLabelID.Name].ReadOnly = true;
                this.dgvSet.Columns[colDilutionFactor.Name].ReadOnly = true;
            }
        }

    

        private void FillGrid()
        {
            string strSQL = "select count(*) from IgEDetectDetail Where HdrRecID='" + mstrHdrRecID + "' and SickPersonID!=''";
            bool blnInitPerson = CvtFunc.ToInt ( mdbcon.GetValue ( strSQL ))> 0? false : true ;
            
            strSQL = "select * from IgEDetectDetail Where HdrRecID='" + mstrHdrRecID + "' order by recid";
            DataTable dtFill = mdbcon.GetDataTable(strSQL);
            foreach (DataRow drFill in dtFill.Rows)
            {
                this.dgvSet.Rows.Add();
                this.dgvSet.Rows[this.dgvSet.Rows.Count - 1].Cells[colSeqNo.Name].Value = this.dgvSet.Rows.Count;
                this.dgvSet.Rows[this.dgvSet.Rows.Count - 1].Cells[colSickRecID.Name].Value = CvtFunc.ToString(drFill["SickPersonID"]);
                this.dgvSet.Rows[this.dgvSet.Rows.Count - 1].Cells[colRow.Name].Value = CvtFunc.ToInt(drFill["Rows"]) +1 ;
                this.dgvSet.Rows[this.dgvSet.Rows.Count - 1].Cells[colCharRow.Name].Value =BaseFunctions.SetCharRow( (CvtFunc.ToInt(drFill["Rows"]) +1 ).ToString()) ;
                this.dgvSet.Rows[this.dgvSet.Rows.Count - 1].Cells[colCols.Name].Value = CvtFunc.ToInt(drFill["Cols"]) +1 ;
                this.dgvSet.Rows[this.dgvSet.Rows.Count - 1].Cells[colDilutionFactor.Name].Value = BaseFunctions.FormatNumeric(  CvtFunc.ToDouble(drFill["DilutionFactor"]),"N.2");
                this.dgvSet.Rows[this.dgvSet.Rows.Count - 1].Cells[colLabelID.Name].Value = CvtFunc.ToString(drFill["DetectLabel"]);
                this.dgvSet.Rows[this.dgvSet.Rows.Count - 1].Cells[colSickName.Name].Value = CvtFunc.ToString(mdbcon.GetValue("select sickname from mSickPerson Where SickRecID='" + drFill["SickPersonID"] + "'"));
            }
            this.dgvSet.EndEdit();
            if (blnInitPerson)
            {
                string strCon = "";
                if (mFormulaType == FormulaType.TotalIgG4)
                {
                    strCon = " NeedTotalIgG4 = 1 and TotalIgG4ID=''";
                }
                else if (mFormulaType == FormulaType.IgG4)
                {
                    strCon = " NeedIgG4 = 1 and IgG4ID=''";
                }
                else if (mFormulaType == FormulaType.IgE)
                {
                    strCon = " NeedIgE = 1 and IgEID=''";
                }

                int intRow = 0;
                strSQL = "Select * from IgETreatHistory Where {0} Order by RecID";
                strSQL = string.Format(strSQL, strCon);
                DataTable dtTreat = mdbcon.GetDataTable(strSQL);
                foreach (DataRow drTreat in dtTreat.Rows)
                {
                    this.dgvSet.Rows[intRow].Cells[colSickRecID.Name].Value = drTreat["SickRecID"];
                    this.dgvSet.Rows[intRow].Cells[colSickName.Name].Value = CvtFunc.ToString(mdbcon.GetValue("select sickname from mSickPerson Where SickRecID='" + drTreat["SickRecID"] + "'"));
                }
                this.dgvSet.EndEdit();             
            }
            this.dgvSet.Columns["colSickRecID"].Width = 200;
            this.dgvSet.Columns["colSickName"].Width = 80;
                
        }

        private void dgvSet_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == colSickName.Index)
            {
                string strSQL="select RecID,SickRecID as 患者编号,SickName as 姓名, Sex as 性别 from mSickPerson Where Type='IgE' ";
                frmLookUp_Cust frmlookup = new frmLookUp_Cust( mdbcon,strSQL ,"患者选择","","SickRecID");
                frmlookup.ShowDialog(); 
                if (frmlookup.HaveReturnValue)
                {
                    this.dgvSet.Rows[e.RowIndex].Cells[colSickRecID.Name].Value = frmlookup.GetReturnValue("SickRecID");
                    this.dgvSet.Rows[e.RowIndex].Cells[colSickName.Name].Value = frmlookup.GetReturnValue("SickName");
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.dgvSet.EndEdit();
            if (UpdateSickToIgEDetectdetail())
            {
                this.Close(); 
            }
        }

        private bool UpdateSickToIgEDetectdetail()
        {
            try
            {
                string strSQL = "";
                using (DBAccess dbcon = new DBAccess(false, false))
                {
                    foreach (DataGridViewRow dgvRow in this.dgvSet.Rows)
                    {
                        strSQL = "update IgEDetectdetail Set SickPersonID='{0}',DetectLabel='{1}',DilutionFactor='{4}' where Rows='{2}' and Cols='{3}' and HdrRecID='{5}'";
                        strSQL = string.Format(strSQL, CvtFunc.ToString(dgvRow.Cells["colSickRecID"].Value)
                                                        , CvtFunc.ToString(dgvRow.Cells["colLabelID"].Value)
                                                        , CvtFunc.ToLong(dgvRow.Cells["colRow"].Value)-1
                                                        , CvtFunc.ToLong(dgvRow.Cells["colCols"].Value)-1
                                                        , CvtFunc.ToDouble(dgvRow.Cells[colDilutionFactor.Name].Value)      
                                                        , mstrHdrRecID);
                        dbcon.ExecuteSql(strSQL); 
                    }
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
